<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="100px">
      <el-form-item label="优惠券名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入优惠券名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
<!--      <el-form-item label="条件规则id" prop="conditionId">-->
<!--        <el-input-->
<!--          v-model="queryParams.conditionId"-->
<!--          placeholder="请输入条件规则id"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="所属积分" prop="point">-->
<!--        <el-input-->
<!--          v-model="queryParams.point"-->
<!--          placeholder="请输入所属积分"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="叠加 0是 1否" prop="overlay">-->
<!--        <el-input-->
<!--          v-model="queryParams.overlay"-->
<!--          placeholder="请输入叠加 0是 1否"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="有效期 0一个月 1三个月 2 半年  3 一年  4永久" prop="period">-->
<!--        <el-input-->
<!--          v-model="queryParams.period"-->
<!--          placeholder="请输入有效期 0一个月 1三个月 2 半年  3 一年  4永久"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="可领取次数" prop="times">-->
<!--        <el-input-->
<!--          v-model="queryParams.times"-->
<!--          placeholder="请输入可领取次数"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="有效期单位 天、周、月、年" prop="periodUnit">-->
<!--        <el-input-->
<!--          v-model="queryParams.periodUnit"-->
<!--          placeholder="请输入有效期单位 天、周、月、年"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['charge:coupon:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['charge:coupon:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['charge:coupon:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['charge:coupon:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="couponList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="表主键id" align="center" prop="id" />
      <el-table-column label="优惠券名称" align="center" prop="name" />
      <el-table-column label="条件规则id" align="center" prop="conditionId" />
      <el-table-column label="所属积分" align="center" prop="point" />
      <el-table-column label="类型" align="center" prop="userType" #default="scp">
        <el-tag :type="scp.row.userType === 0 ? 'success' : 'warning'">
          {{ scp.row.userType === 0 ? '通用' : '专用' }}
        </el-tag>
      </el-table-column>
      <el-table-column label="叠加" align="center" prop="overlay" #default="scp">
        <el-tag :type="scp.row.overlay === 0 ? 'success' : 'info'">
          {{ scp.row.overlay === 0 ? '是' : '否' }}
        </el-tag>
      </el-table-column>
      <el-table-column label="有效期" align="center" prop="period" #default="scp">
        <el-tag :type="scp.row.period === 4 ? 'danger' : scp.row.period === 3 ? 'warning' : scp.row.period === 2 ? 'success' : scp.row.period === 1 ? 'primary' : 'info'">
          {{ scp.row.period === 0 ? '一个月' : scp.row.period === 1 ? '三个月' : scp.row.period === 2 ? '半年' : scp.row.period === 3 ? '一年' : '永久' }}
        </el-tag>
      </el-table-column>
      <el-table-column label="可领取次数" align="center" prop="times" />
      <el-table-column label="有效期单位" align="center" prop="periodUnit" #default="scp">
        <el-tag :type="scp.row.periodUnit === 0 ? 'info' : scp.row.periodUnit === 1 ? 'primary' : scp.row.periodUnit === 2 ? 'success' : 'warning'">
          {{ scp.row.periodUnit === 0 ? '天' : scp.row.periodUnit === 1 ? '周' : scp.row.periodUnit === 2 ? '月' : '年' }}
        </el-tag>
      </el-table-column>
        <el-table-column label="是否还可以用" align="center" prop="status" #default="scp">
          <el-tag :type="scp.row.status === 0 ? 'success' : 'danger'">
            {{ scp.row.status === 0 ? '是' : '否' }}
          </el-tag>
        </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['charge:coupon:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['charge:coupon:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改优惠券对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="110px">
        <el-form-item label="优惠券名称:" prop="name">
          <el-input v-model="form.name" placeholder="请输入优惠券名称" />
        </el-form-item>
        <el-form-item label="条件规则id:" prop="conditionId">
          <el-input v-model="form.conditionId" placeholder="请输入条件规则id" />
        </el-form-item>
        <el-form-item label="所属积分:" prop="point">
          <el-input v-model="form.point" placeholder="请输入所属积分" />
        </el-form-item>
        <el-form-item label="类型:" prop="userType">
          <el-input v-model="form.userType" placeholder="请输入类型 0通用 1专用" />
        </el-form-item>
        <el-form-item label="叠加:" prop="overlay">
          <el-input v-model="form.overlay" placeholder="请输入叠加 0是 1否" />
        </el-form-item>
        <el-form-item label="有效期:" prop="period">
          <el-input v-model="form.period" placeholder="请输入有效期 0一个月 1三个月 2 半年  3 一年  4永久" />
        </el-form-item>
        <el-form-item label="可领取次数:" prop="times">
          <el-input v-model="form.times" placeholder="请输入可领取次数" />
        </el-form-item>
        <el-form-item label="有效期单位:" prop="periodUnit">
          <el-input v-model="form.periodUnit" placeholder="请输入有效期单位 天、周、月、年" />
        </el-form-item>
        <el-form-item label="是否还可以用:" prop="status">
          <el-input v-model="form.status" placeholder="请输入是否还可以用 0是 1否" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listCoupon, getCoupon, delCoupon, addCoupon, updateCoupon } from "@/api/charge/coupon";

export default {
  name: "Coupon",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 优惠券表格数据
      couponList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: null,
        conditionId: null,
        point: null,
        userType: null,
        overlay: null,
        period: null,
        times: null,
        periodUnit: null,
        status: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        name: [
          { required: true, message: "优惠券名称不能为空", trigger: "blur" }
        ],
        conditionId: [
          { required: true, message: "条件规则id不能为空", trigger: "blur" }
        ],
        point: [
          { required: true, message: "所属积分不能为空", trigger: "blur" }
        ],
        userType: [
          { required: true, message: "类型 0通用 1专用不能为空", trigger: "change" }
        ],
        overlay: [
          { required: true, message: "叠加 0是 1否不能为空", trigger: "blur" }
        ],
        period: [
          { required: true, message: "有效期 0一个月 1三个月 2 半年  3 一年  4永久不能为空", trigger: "blur" }
        ],
        times: [
          { required: true, message: "可领取次数不能为空", trigger: "blur" }
        ],
        periodUnit: [
          { required: true, message: "有效期单位 天、周、月、年不能为空", trigger: "blur" }
        ],
        status: [
          { required: true, message: "是否还可以用 0是 1否不能为空", trigger: "change" }
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询优惠券列表 */
    getList() {
      this.loading = true;
      listCoupon(this.queryParams).then(response => {
        this.couponList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        name: null,
        conditionId: null,
        point: null,
        userType: null,
        overlay: null,
        period: null,
        times: null,
        periodUnit: null,
        status: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加优惠券";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getCoupon(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改优惠券";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateCoupon(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addCoupon(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除优惠券编号为"' + ids + '"的数据项？').then(function() {
        return delCoupon(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('charge/coupon/export', {
        ...this.queryParams
      }, `coupon_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
